package ter.find.pattern.gui;


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import com.ter.Form;

import ter.find.pattern.ModelCharge;
import ter.find.pattern.ModelMotif;


import jxl.Cell;

import jxl.CellType;
import jxl.DateCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class LireFichierXLS extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTable table;
	private JButton btload;
	private DefaultTableModel tableModel;
	private JTextField txtField1;
	private JFileChooser fc;
	Workbook w;
	String date,time;
	private JButton bdiscret;
	private JSpinner spinPage;
	private JLabel labpage;
	public String path;
	public LireFichierXLS() {
		createGUI();
	}

	private void createGUI() {
		setLayout(new BorderLayout());
		
		/*page et discretisation*/
		spinPage=new JSpinner();
		labpage=new JLabel("N° Page");
		new JLabel("Fichier");

		bdiscret=new JButton("Valider");
		bdiscret.setPreferredSize(new Dimension(50, 20));
		JScrollPane pane = new JScrollPane();
		table = new JTable();
		table.setDefaultRenderer(Object.class, new TableColorLine());
		pane.setViewportView(table);
		pane.setBorder(new TitledBorder("Données de Température"));

		JPanel progress=new JPanel();
		progress.setBorder(new TitledBorder(""));

		JPanel eastPanel = new JPanel();
		eastPanel.setBorder(new TitledBorder("Menu"));
		eastPanel.setLayout(new BorderLayout());
		btload  = new JButton("Parcourir...");
		btload.setPreferredSize(new Dimension(100, 20));
		JPanel panbout=new JPanel(new BorderLayout());
		panbout.add(labpage, BorderLayout.NORTH);
		panbout.add(spinPage, BorderLayout.CENTER);
		panbout.add(btload, BorderLayout.SOUTH);
		//eastPanel.add(spinPage,BorderLayout.BEFORE_LINE_BEGINS);
		eastPanel.add(panbout,BorderLayout.NORTH);
		eastPanel.add(bdiscret,BorderLayout.AFTER_LAST_LINE);
		btload.setPreferredSize(new Dimension(100, 20));
		spinPage.setPreferredSize(new Dimension(50, 20));
		bdiscret.setPreferredSize(new Dimension(150, 50));
		
		JPanel northPanel = new JPanel();
		txtField1 = new JTextField();
		JLabel lblField1 = new JLabel("Fichier :   ");
		northPanel.add(lblField1);
		northPanel.add(txtField1);
		txtField1.setPreferredSize(new Dimension(300, 20));

		add(northPanel, BorderLayout.NORTH);
		add(eastPanel, BorderLayout.EAST);
		add(pane,BorderLayout.CENTER);
		add(progress,BorderLayout.SOUTH);
		elementCenter(table);
		tableModel = new DefaultTableModel(new Object[]{"Date","Heure","Temperature"},0);
		table.setModel(tableModel);

		btload.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				try {
					btnBrowseActionPerformed(evt);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		bdiscret.addActionListener(new MyActionListener());
	}

	
	private void btnBrowseActionPerformed(java.awt.event.ActionEvent evt) throws IOException {
		if (fc == null) {
			fc = new JFileChooser(".");
		}
		// Show it.
		int returnVal = fc.showOpenDialog(this);

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File inputWorkbook = new File(fc.getSelectedFile().getPath());
			path=fc.getSelectedFile().getPath();
			txtField1.setText(fc.getSelectedFile().getPath());
			try {
			
				w = Workbook.getWorkbook(inputWorkbook);
				Sheet sheet = w.getSheet((Integer)spinPage.getValue());// 0 numero de feuille à lire

				System.out.println("taille : "+ sheet.getRows());
				System.out.println("\n");
				//int j=0;
				for (int i = 4; i < sheet.getRows(); i++) {
					Cell cellG = sheet.getCell(0, i);
					Cell cellD = sheet.getCell(1, i);

					if(cellG.getType()==CellType.DATE){
						DateCell dCell = (DateCell) cellG;  

						TimeZone gmtZone = TimeZone.getTimeZone("GMT");  
						DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyy");
						DateFormat year=new SimpleDateFormat("yyy");
						dateFormat.setTimeZone(gmtZone);
						year.setTimeZone(gmtZone);
						date = dateFormat.format(dCell.getDate());	
						year.format(dCell.getDate());
						if(i==4){
						}
						
						DateFormat TimeFormat = new SimpleDateFormat("HH:mm");  
						TimeFormat.setTimeZone(gmtZone);
						time = TimeFormat.format(dCell.getDate()); 
						 
					}
					//double tmpMax=Double.parseDouble(cellD.getContents());
					tableModel.addRow(new Object[]{date,time,cellD.getContents()});

				}

				System.out.println("\n");
			} catch (BiffException e) {
				JOptionPane.showMessageDialog(null, "No Data found at page "+spinPage.getValue());
				e.printStackTrace();
			}

		} else {
			txtField1.setText("");
		}	
		fc.setSelectedFile(null);


	}

	class MyActionListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			Object selec = e.getSource();
			if(selec.equals(bdiscret)){
				Form.updatepath(txtField1.getText());
				Form.load(path);
				JOptionPane.showMessageDialog(null, "Vous pouvez maintenant discrétiser les valeurs!!");
			}
			
			
		}
		
	}


	public void elementCenter(JTable tab){
		DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
		centerRenderer.setHorizontalAlignment( JLabel.CENTER );
		tab.setDefaultRenderer(String.class, centerRenderer);
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame("TabbedPane Source Demo");
		frame.addWindowListener(new WindowAdapter() {

			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		new ControlPanel();
		new ModelMotif();
		new ModelCharge();


		new ChangeForm();
		//Gui g=new Gui(c, mp,cp);
		//InterfaceEngine i=new InterfaceEngine(m,g,c,mp);
		frame.getContentPane().add(new LireFichierXLS(),
				BorderLayout.CENTER);
		frame.pack();
		frame.setSize(1050,600);
		frame.setVisible(true);
	}

} 

